package com.offcn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.offcn.mapper.MemberMapper;
import com.offcn.mapper.OrderMapper;
import com.offcn.mapper.OrdersettingMapper;
import com.offcn.mapper.SetmealMapper;
import com.offcn.pojo.Member;
import com.offcn.pojo.Order;
import com.offcn.pojo.Ordersetting;
import com.offcn.pojo.Setmeal;
import com.offcn.service.OrderService;
import com.offcn.utils.DateUtils;
import com.offcn.utils.MessageConstant;
import com.offcn.utils.Result;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDate;
import java.util.*;

/**
 * @program: offcnpe_parent
 * @description:
 * @author: xin yi
 * @create: 2021-07-21 10:46
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrdersettingMapper ordersettingMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public Result m_submitOrder(Map map) {
        /*查询数据库中有没有预约的日期*/
        try {
            Date orderDate = DateUtils.parseString2Date((String) map.get("orderDate"));

            QueryWrapper<Ordersetting> ordersettingQueryWrapper = new QueryWrapper<>();
            ordersettingQueryWrapper.eq("orderDate", orderDate);
            Ordersetting ordersetting = ordersettingMapper.selectOne(ordersettingQueryWrapper);
            /*没有找到对应的预约日期*/
            if (ordersetting == null) {
                return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
            }
            /*判断该日期是否预约已满*/
            if (ordersetting.getReservations() >= ordersetting.getNumber()) {
                return new Result(false, MessageConstant.ORDER_FULL);
            }

            /*根据手机号码查询会员表中已经存在的用户信息*/
            QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();
            memberQueryWrapper.eq("phoneNumber", (String) map.get("telephone"));
            List<Member> memberList = memberMapper.selectList(memberQueryWrapper);
            Member member = null;
            /*判断非会员*/
            if (memberList.size() == 0) {
                System.out.println("进入了判断是否为会员方法");
                member = new Member();
                member.setIdcard((String) map.get("idCard"));
                member.setName((String) map.get("name"));
                member.setSex((String) map.get("sex"));
                member.setPhonenumber((String) map.get("telephone"));
                /*时间还得转换*/
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(new Date().getTime());
                member.setRegtime(LocalDate.of(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH)));
                memberMapper.insert(member);

                System.out.println(member.getName() + "刚成为会员");
            } else {
                member = memberList.get(0);
                System.out.println(member.getName() + "是会员");
            }

            /*会员*/
//            Member member = memberList.get(0);
            Integer setmealId = (Integer.parseInt((String) map.get("setmealId")));

            QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
            orderQueryWrapper.eq("member_id", member.getId())
                    .eq("orderDate", orderDate)
                    .eq("setmeal_id", setmealId);
            List<Order> orderList = orderMapper.selectList(orderQueryWrapper);
            /*在同一天同一个人预约了多次同一个套餐*/
            if (orderList != null && orderList.size() > 0) {
                return new Result(false, MessageConstant.HAS_ORDERED);
            }
            /*预约人数+1*/
            ordersetting.setReservations(ordersetting.getReservations() + 1);
            /*减完再更新一次*/
            ordersettingMapper.updateById(ordersetting);

            /*预约成功保存预约信息*/
            Order order = new Order();
            order.setMemberId(member.getId());
            order.setOrdertype("微信预约");
            order.setOrderstatus("未到诊");
            order.setSetmealId(setmealId);
            /*得把util date 转成localdate*/
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(orderDate.getTime());
            order.setOrderdate(LocalDate.of(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH)));
            int row = orderMapper.insert(order);
            if (row > 0) {
                return new Result(true, MessageConstant.ORDERSETTING_SUCCESS, member.getId());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.ORDERSETTING_FAIL);
    }


    @Override
    public Result m_findOrderById(Integer id) {
        System.out.println(id);
        Member member = memberMapper.selectById(id);
        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.eq("member_id",id);
        Order order = orderMapper.selectOne(orderQueryWrapper);
        Setmeal setmeal = setmealMapper.selectById(order.getSetmealId());
        Map<String,Object> orderInfo = new HashMap<>();
        orderInfo.put("member",member.getName());
        orderInfo.put("setmeal",setmeal.getName());
        orderInfo.put("orderDate",order.getOrderdate());
        orderInfo.put("orderType",order.getOrdertype());
        if (order != null){
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,orderInfo);
        }
        return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
    }
}
